package com.gitee.melin.bee.core.jdbc.relational;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
public class MetaTable implements Serializable, Comparable<MetaTable> {

    private static final long serialVersionUID = 4209205512472367171L;

    private String catalogName;

    private String schemaName;

    private String tableName;

    private String location;

    private String comment;

    private String tableType;

    private String engine;

    private String options;

    private Long dataLength;

    private Long rows;

    private Date createTime;

    private Date updateTime;

    private List<MetaColumn> columns;

    private List<String> partitionKeys = new ArrayList<>();

    private Map<String, Object> properties;

    public MetaTable() {
    }

    public MetaTable(String tableName) {
        this.tableName = tableName;
    }

    public MetaTable(String schemaName, String tableName) {
        this.schemaName = schemaName;
        this.tableName = tableName;
    }

    public String getSchemaTableName() {
        return StringUtils.isBlank(schemaName) ? tableName : schemaName + "." + tableName;
    }

    public List<String> getPrimaryKeys() {
        return columns.stream()
                .filter(MetaColumn::isPrimaryKey)
                .map(MetaColumn::getColumnName)
                .collect(Collectors.toList());
    }

    @Override
    public int compareTo(MetaTable o) {
        return this.tableName.compareTo(o.getTableName());
    }
}
